package com.lgmshare.k3.map;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.Toast;

import com.lgmshare.component.logger.Logger;
import com.lgmshare.k3.R;
import com.lgmshare.component.utils.ContextUtils;

import java.net.URISyntaxException;

/**
 * 地图导航帮助类
 * <p/>
 * Created by LiBinWen on 2015/11/16.
 */
public class MapNavigationHelper {

    private static final String TAG = "MapNavigationHelper";

    /**
     * 百度导航
     */
    public interface BaiduMap {
        String PACKAGE_NAME = "com.baidu.BaiduMap";
        String MODE_DERIVING = "driving";
        String MODE_TRANSIT = "transit";
        String MODE_WALKING = "walking";
    }

    /**
     * 腾讯导航
     */
    public interface TencentMap {
        String PACKAGE_NAME = "com.tencent.map";
        String MODE_BUS = "bus";
        String MODE_DRIVE = "drive";
        String MODE_WALK = "walk";
    }

    /**
     * 高德导航
     */
    public interface AutoMap {
        String PACKAGE_NAME = "com.autonavi.minimap";

        // t = 0（驾车）= 1（公交）= 2（步行）= 3（骑行）= 4（火车）= 5（长途客车）（骑行仅在V7.8.8以上版本支持）
        int MODE_DRIVE = 0;
        int MODE_BUS = 1;
        int MODE_WALK = 2;
        String AUTO_MAP_CATAGORY = "android.intent.category.DEFAULT";
    }

    /**
     * 检测百度地图APP是否已安装
     *
     * @param context
     * @return
     */
    public static boolean isBaiduMapInstalled(Context context) {
        return ContextUtils.isAppInstalled(context, BaiduMap.PACKAGE_NAME);
    }

    /**
     * 检测腾讯地图APP是否已安装
     *
     * @param context
     * @return
     */
    public static boolean isTencentMapInstalled(Context context) {
        return ContextUtils.isAppInstalled(context, TencentMap.PACKAGE_NAME);
    }

    /**
     * 检测高德地图APP是否已安装
     *
     * @param context
     * @return
     */
    public static boolean isAutoMapInstalled(Context context) {
        return ContextUtils.isAppInstalled(context, AutoMap.PACKAGE_NAME);
    }

    /**
     * 百度线路规划
     * TODO 目的地的经纬度与地址描述不能同时为空
     *
     * @param context
     * @param toLat   目的纬度
     * @param toLon   目的经度
     * @param toAddr  目的地址详情
     * @param mode    模式
     */
    public static void startBaiduMapRoutePlan(Context context, double toLat, double toLon, String toAddr, String mode) {
        startBaiduMapRoutePlan(context, 0, 0, null, toLat, toLon, toAddr, mode);
    }

    /**
     * 百度线路规划
     * TODO 起始地和目的地的经纬度与地址描述不能同时为空
     *
     * @param context
     * @param fromLat  起始纬度
     * @param fromLon  起始经度
     * @param fromAddr 起始地址信息
     * @param toLat    目的纬度
     * @param toLon    目的经度
     * @param toAddr   目的地址详情
     * @param mode     模式
     */
    public static void startBaiduMapRoutePlan(Context context, double fromLat, double fromLon, String fromAddr, double toLat, double toLon, String toAddr, String mode) {
        if (!isBaiduMapInstalled(context)) {
            Toast.makeText(context, "百度地图应用未安装！", Toast.LENGTH_LONG).show();
            return;
        }

        if (toLat == 0 || toLon == 0 || TextUtils.isEmpty(toAddr)) {
            Toast.makeText(context, "路径规划目的地为空！", Toast.LENGTH_LONG).show();
            return;
        }

        StringBuilder builder = new StringBuilder("intent://map/direction?");
        builder.append("src=").append(context.getResources().getString(R.string.app_name));
        builder.append("&mode=").append(mode);//导航方式
        //起点坐标
        if (fromLat != 0 && fromLon != 0) {
            builder.append("&origin=latlng:");
            builder.append(fromLat).append(",").append(fromLon);
            builder.append("|name:").append(fromAddr);
        }
        //终点坐标
        builder.append("&destination=latlng:");
        builder.append(toLat).append(",").append(toLon);
        builder.append("|name:").append(toAddr);
        builder.append("#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");

        try {
            String uri = builder.toString();
            Logger.d(TAG, uri);
            Intent intent = Intent.parseUri(uri, 0);
            context.startActivity(intent);
        } catch (URISyntaxException e) {
            e.printStackTrace();
            return;
        }
    }

    /**
     * 腾讯线路规划
     *
     * @param context
     * @param toLat   目的纬度
     * @param toLon   目的经度
     * @param toAddr  目的地址详情
     * @param mode    模式
     */
    public static void startTencentMapRoutePlan(Context context, double toLat, double toLon, String toAddr, String mode) {
        startTencentMapRoutePlan(context, 0, 0, null, toLat, toLon, toAddr, mode);
    }

    /**
     * 腾讯线路规划
     * TODO 目的地的经纬度与地址描述不能同时为空
     *
     * @param context
     * @param fromLat
     * @param fromLon
     * @param fromAddr
     * @param toLat
     * @param toLon
     * @param toAddr
     * @param mode     驾车：type=drive
     */
    public static void startTencentMapRoutePlan(Context context, double fromLat, double fromLon, String fromAddr, double toLat, double toLon, String toAddr, String mode) {
        if (!isTencentMapInstalled(context)) {
            Toast.makeText(context, "未安装腾讯地图！", Toast.LENGTH_LONG).show();
            return;
        }

        if (TextUtils.isEmpty(toAddr)) {
            Toast.makeText(context, "路径规划目的地为空！", Toast.LENGTH_LONG).show();
            return;
        }

        StringBuilder builder = new StringBuilder("qqmap://map/routeplan?");
        builder.append("referer=").append(context.getString(R.string.app_name));
        builder.append("&type=").append(mode);
        //policy:有以下取值
        //0：较快捷
        //1：无高速
        //2：距离
        //policy的取值缺省为0
        builder.append("&policy=0");

        //起点坐标
        if (fromLat != 0 && fromLon != 0) {
            builder.append("&from=").append(fromAddr);
            builder.append("&fromcoord=").append(fromLat).append(",").append(fromLon);
        }

        builder.append("&to=").append(toAddr);
        builder.append("&tocoord=").append(toLat).append(",").append(toLon);

        try {
            String uri = builder.toString();
            Logger.d(TAG, uri);
            Intent intent = Intent.parseUri(uri, 0);
            context.startActivity(intent);
        } catch (URISyntaxException e) {
            e.printStackTrace();
            return;
        }
    }

    /**
     * 高德地图线路规划
     * 目的地的经纬度与地址描述不能同时为空
     *
     * @param context
     * @param toLat   目的纬度
     * @param toLon   目的经度
     * @param toAddr  目的地址详情
     * @param mode    t = 0（驾车）= 1（公交）= 2（步行）= 3（骑行）
     */
    public static void startAutoMapRoutePlan(Context context, double toLat, double toLon, String toAddr, int mode) {
        startAutoMapRoutePlan(context, 0, 0, null, toLat, toLon, toAddr, mode);
    }

    /**
     * 高德地图线路规划
     *
     * @param context
     * @param fromLat
     * @param fromLng
     * @param startAddr
     * @param toLat
     * @param toLon
     * @param toAddr
     * @param mode      t = 0（驾车）= 1（公交）= 2（步行）= 3（骑行）
     */
    public static void startAutoMapRoutePlan(final Context context, double fromLat, double fromLng, final String startAddr, double toLat, double toLon, final String toAddr, int mode) {
        if (!isAutoMapInstalled(context)) {
            Toast.makeText(context, "高德地图应用未安装！", Toast.LENGTH_LONG).show();
            return;
        }

        if (toLat == 0 || toLon == 0 || TextUtils.isEmpty(toAddr)) {
            Toast.makeText(context, "路径规划目的地为空！", Toast.LENGTH_LONG).show();
            return;
        }

        StringBuilder builder = new StringBuilder("amapuri://route/plan?");
        builder.append("sourceApplication=").append(context.getString(R.string.app_name));
        builder.append("&dev=0");//是否偏移(0:lat 和 lon 是已经加密后的,不需要国测加密; 1:需要国测加密)
        builder.append("&t=").append(mode);

        //起始地址
        if (fromLat != 0 && fromLng != 0) {
            builder.append("&slat=").append(fromLat);
            builder.append("&slon=").append(fromLng);
            builder.append("&sname=").append(startAddr);
        }

        //终点地址
        builder.append("&dlat=").append(toLat);
        builder.append("&dlon=").append(toLon);
        builder.append("&dname=").append(toAddr);

        String uri = builder.toString();
        Logger.d(TAG, uri);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setPackage(AutoMap.PACKAGE_NAME);
        intent.addCategory(AutoMap.AUTO_MAP_CATAGORY);
        intent.setData(Uri.parse(uri));
        context.startActivity(intent);
    }
}
